package com.example.shirodemo.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

/**
 * ShiroTest   com.example.shirodemo.realm
 *
 * @author：黄玉刚 -成都电安惠科有限公司
 * @date： 2019/11/30 22:31
 * @Description ：
 */
public class SecondRealm extends AuthenticatingRealm {

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("[Second Realm]");

        UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token;
        String username=usernamePasswordToken.getUsername();
        System.out.println("从数据库中获取UserName信息2"+username+"  "+new String(usernamePasswordToken.getPassword()));
        if (!"admin".equals(username)) {
            throw new UnknownAccountException("账户不存在！");
        }
        //1） pricipal :认证的实体信息，可以是user那么，也可以是实体对象
        //2） credential：密码
        //3） realname:当前realm对象的name，调用父类的getName()方法即可

        ByteSource credentialsSalt = ByteSource.Util.bytes("123");

        Object saltPassword=  new SimpleHash("SHA1",new String(usernamePasswordToken.getPassword()),credentialsSalt,5);

        return new SimpleAuthenticationInfo(username, saltPassword.toString(),credentialsSalt, getName());


    }
}
